package com.xy.tool.report.word;

import com.xy.tool.report.view.Row;
import com.xy.tool.report.view.Table;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.springframework.util.CollectionUtils;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by zsp on 2019/5/16.
 */
public class WordTemplate {

    public XWPFDocument create(List<Table> tableList, int rows, int cols) {
        if(CollectionUtils.isEmpty(tableList)) {
            throw new IllegalArgumentException();
        }
        XWPFDocument document = new XWPFDocument();
        for(Table table : tableList) {
            createTable(table, rows, cols, document);
        }
        return document;
    }

    protected void createTable(Table table, int rows, int cols, XWPFDocument document) {
        XWPFTable wordTable = document.createTable(rows, cols);

    }

    protected void createRow(List<Row> rowList, XWPFTable wordTable) {
        XWPFTableRow wordRow = wordTable.createRow();

    }

    protected void createCell(XWPFTableRow wordRow) {
        XWPFTableCell wordCell = wordRow.createCell();

    }

    public  void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
        for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
            XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
            if ( cellIndex == fromCell ) {
                // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
            } else {
                // Cells which join (merge) the first one, are set with CONTINUE
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
            }
        }
    }

    public void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
        for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
            XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
            if (rowIndex == fromRow) {
                // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
            } else {
                // Cells which join (merge) the first one, are set with CONTINUE
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
            }
        }
    }

    private void getParagraph(XWPFTableCell cell,String cellText){
        CTP ctp = CTP.Factory.newInstance();
        XWPFParagraph p = new XWPFParagraph(ctp, cell);
        p.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun run = p.createRun();
        run.setText(cellText);
        CTRPr rpr = run.getCTR().isSetRPr() ? run.getCTR().getRPr() : run.getCTR().addNewRPr();
        CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts();
        fonts.setAscii("仿宋");
        fonts.setEastAsia("仿宋");
        fonts.setHAnsi("仿宋");
        cell.setParagraph(p);
    }

    public static void main(String[] args) throws Exception {
        WordTemplate wordTemplate = new WordTemplate();

        XWPFDocument document = new XWPFDocument();
        XWPFTable wordTable = document.createTable(5, 5);
        //wordTable.addNewCol();
        //XWPFTableRow wordRow = wordTable.createRow();
        //XWPFTableCell wordCell = wordRow.createCell();
        wordTable.getRow(2).getCell(2).setText("hello");
        wordTemplate.mergeCellsHorizontal(wordTable, 3, 0, 2);
        //wordTemplate.mergeCellsVertically(wordTable, 0, 0, 1);

        OutputStream stream = new FileOutputStream("F:\\abc.docx");
        document.write(stream);
        stream.close();
    }

}
